package geektime.spring.springbucks.controller;

import geektime.spring.springbucks.entity.TCoffee;
import geektime.spring.springbucks.service.TCoffeeService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * (TCoffee)表控制层
 *
 * @since 2022-10-30 00:14:38
 */
@RestController
@RequestMapping("tCoffee")
public class TCoffeeController {
    /**
     * 服务对象
     */
    @Resource
    private TCoffeeService tCoffeeService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @PostMapping("/queryByPage/{pageNum:\\d+}:{pageSize:\\d+}")
    public ResponseEntity<List<TCoffee>> queryByPage(@RequestBody Map<String, String> args, @PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize) {
        return ResponseEntity.ok(this.tCoffeeService.queryByPage(args, pageNum, pageSize));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("/queryById/{id}")
    public ResponseEntity<TCoffee> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.tCoffeeService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param tCoffee 实体
     * @return 新增结果
     */
    @PostMapping("addCoffee")
    public ResponseEntity<TCoffee> add(@RequestBody TCoffee tCoffee) {
        return ResponseEntity.ok(this.tCoffeeService.insert(tCoffee));
    }

    /**
     * 编辑数据
     *
     * @param tCoffee 实体
     * @return 编辑结果
     */
    @PutMapping("modifyCoffee")
    public ResponseEntity<TCoffee> edit(@RequestBody TCoffee tCoffee) {
        return ResponseEntity.ok(this.tCoffeeService.update(tCoffee));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("delete/{id}")
    public ResponseEntity<Boolean> deleteById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.tCoffeeService.deleteById(id));
    }

}

